Java Wrapper Classes - Complete Cheatsheet
A comprehensive guide to Java's wrapper classes with practical code snippets and their use cases.
๐ Quick Navigationโ
- Integer
- Long
- Character
- Boolean
- Double
- Float
- Byte & Short
- String
- Quick Reference Summary
- Best Practices
๐น Integer (for int)โ
Parsing & Conversionโ
// String to int/Integer
Integer.parseInt("123"); // Returns: 123 (primitive int)
Integer.valueOf("123"); // Returns: Integer object
Integer.valueOf("1010", 2); // Binary to int: 10
Integer.decode("0x1A"); // Hex to int: 26
// int to String
Integer.toString(42); // Returns: "42"
Integer.toString(10, 2); // Binary: "1010"
Integer.toString(255, 16); // Hex: "ff"
Math Operationsโ
Integer.max(10, 20); // Returns: 20
Integer.min(10, 20); // Returns: 10
Integer.sum(10, 20); // Returns: 30
Integer.compare(5, 7); // Returns: -1 (since 5 < 7)
Math.abs(-15); // Returns: 15
Bit Operationsโ
Integer.bitCount(7); // Number of 1-bits: 3
Integer.toBinaryString(10); // Returns: "1010"
Integer.toHexString(255); // Returns: "ff"
Integer.toOctalString(8); // Returns: "10"
Integer.highestOneBit(12); // Returns: 8 (1000 in binary)
Integer.numberOfLeadingZeros(8); // Returns: 28
Integer.reverse(0b1100); // Reverses bits
Constantsโ
Integer.MAX_VALUE; // 2,147,483,647
Integer.MIN_VALUE; // -2,147,483,648
Integer.SIZE; // 32 (bits)
๐น Long (for long)โ
Parsing & Conversionโ
// String to long
Long.parseLong("123456789"); // Returns: 123456789L
Long.valueOf("123456789"); // Returns: Long object
Long.decode("0x1FFFFFFFFL"); // Hex to long
// long to String
Long.toString(12345L); // Returns: "12345"
Long.toString(1024L, 2); // Binary: "10000000000"
Math Operationsโ
Long.max(100L, 200L); // Returns: 200L
Long.min(100L, 200L); // Returns: 100L
Long.sum(100L, 200L); // Returns: 300L
Long.compare(5L, 7L); // Returns: -1
Bit Operationsโ
Long.bitCount(15L); // Number of 1-bits: 4
Long.toBinaryString(10L); // Returns: "1010"
Long.toHexString(255L); // Returns: "ff"
Long.numberOfLeadingZeros(16L); // Returns: 59
Long.highestOneBit(12L); // Returns: 8L
Constantsโ
Long.MAX_VALUE; // 9,223,372,036,854,775,807
Long.MIN_VALUE; // -9,223,372,036,854,775,808
Long.SIZE; // 64 (bits)
๐น Character (for char)โ
Character Type Checkingโ
Character.isDigit('5'); // true
Character.isLetter('A'); // true
Character.isLetterOrDigit('7'); // true
Character.isUpperCase('A'); // true
Character.isLowerCase('a'); // true
Character.isWhitespace(' '); // true
Character.isAlphabetic('ฮฑ'); // true (Unicode letters)
Character.isDefined('โฌ'); // true (valid Unicode)
Case Conversionโ
Character.toUpperCase('a'); // Returns: 'A'
Character.toLowerCase('B'); // Returns: 'b'
Character.toTitleCase('a'); // Returns: 'A'
Numeric & Specialโ
Character.getNumericValue('9'); // Returns: 9
Character.getNumericValue('A'); // Returns: 10 (hex)
Character.toString('x'); // Returns: "x"
Character.compare('a', 'b'); // Returns: -1
Constantsโ
Character.MAX_VALUE; // '\uffff' (65535)
Character.MIN_VALUE; // '\u0000' (0)
Character.SIZE; // 16 (bits)
๐น Boolean (for boolean)โ
Parsing & Conversionโ
Boolean.parseBoolean("true"); // Returns: true
Boolean.parseBoolean("TRUE"); // Returns: true
Boolean.parseBoolean("yes"); // Returns: false (only "true" works)
Boolean.valueOf("false"); // Returns: Boolean.FALSE
Boolean.toString(true); // Returns: "true"
Logical Operationsโ
Boolean.logicalAnd(true, false); // Returns: false
Boolean.logicalOr(true, false); // Returns: true
Boolean.logicalXor(true, true); // Returns: false
Boolean.compare(true, false); // Returns: 1
Constantsโ
Boolean.TRUE; // Boolean object for true
Boolean.FALSE; // Boolean object for false
๐น Double (for double)โ
Parsing & Conversionโ
Double.parseDouble("3.14159"); // Returns: 3.14159
Double.valueOf("2.718"); // Returns: Double object
Double.toString(3.14); // Returns: "3.14"
Double.toHexString(1.0); // Returns: "0x1.0p0"
Special Value Checkingโ
Double.isNaN(Math.sqrt(-1)); // true (Not a Number)
Double.isInfinite(1.0 / 0.0); // true
Double.isFinite(3.14); // true
Double.compare(3.5, 4.5); // Returns: -1
Math Operationsโ
Double.max(3.5, 4.5); // Returns: 4.5
Double.min(3.5, 4.5); // Returns: 3.5
Double.sum(2.5, 3.5); // Returns: 6.0
Math.abs(-3.14); // Returns: 3.14
Constantsโ
Double.MAX_VALUE; // 1.7976931348623157E308
Double.MIN_VALUE; // 4.9E-324 (smallest positive)
Double.POSITIVE_INFINITY; // Positive infinity
Double.NEGATIVE_INFINITY; // Negative infinity
Double.NaN; // Not a Number
๐น Float (for float)โ
Parsing & Conversionโ
Float.parseFloat("3.14f"); // Returns: 3.14f
Float.valueOf("2.718f"); // Returns: Float object
Float.toString(3.14f); // Returns: "3.14"
Special Value Checkingโ
Float.isNaN(0.0f / 0.0f); // true
Float.isInfinite(1.0f / 0.0f); // true
Float.isFinite(3.14f); // true
Float.compare(3.5f, 4.5f); // Returns: -1
Constantsโ
Float.MAX_VALUE; // 3.4028235E38
Float.MIN_VALUE; // 1.4E-45
Float.POSITIVE_INFINITY; // Positive infinity
Float.NEGATIVE_INFINITY; // Negative infinity
Float.NaN; // Not a Number
๐น Byte & Shortโ
Byte Operationsโ
Byte.parseByte("127"); // Returns: 127 (byte)
Byte.toString((byte) 65); // Returns: "65"
Byte.valueOf("42"); // Returns: Byte object
Byte.compare((byte) 5, (byte) 7); // Returns: -1
// Constants
Byte.MAX_VALUE; // 127
Byte.MIN_VALUE; // -128
Short Operationsโ
Short.parseShort("32767"); // Returns: 32767 (short)
Short.toString((short) 1000); // Returns: "1000"
Short.valueOf("500"); // Returns: Short object
Short.compare((short) 5, (short) 7); // Returns: -1
// Constants
Short.MAX_VALUE; // 32767
Short.MIN_VALUE; // -32768
๐น String (Special Reference Type)โ
Length & Checkingโ
String s = " Hello World ";
s.length(); // Returns: 15
s.isEmpty(); // Returns: false
"".isEmpty(); // Returns: true
s.isBlank(); // Returns: false (Java 11+)
" ".isBlank(); // Returns: true (Java 11+)
Content Checkingโ
s.contains("Hello"); // Returns: true
s.startsWith(" He"); // Returns: true
s.endsWith("ld "); // Returns: true
s.equals(" Hello World "); // Returns: true
s.equalsIgnoreCase(" HELLO world "); // Returns: true
Manipulationโ
s.trim(); // Returns: "Hello World"
s.strip(); // Returns: "Hello World" (Java 11+)
s.toUpperCase(); // Returns: " HELLO WORLD "
s.toLowerCase(); // Returns: " hello world "
s.replace("World", "Java"); // Returns: " Hello Java "
s.replaceAll("\\s+", " "); // Regex: replace multiple spaces with single space
s.substring(2, 7); // Returns: "Hello"
s.repeat(3); // Returns: " Hello World Hello World Hello World " (Java 11+)
Splitting & Joiningโ
s.split(" "); // Returns: ["", "", "Hello", "World", "", ""]
s.split("\\s+"); // Split on any whitespace: ["", "Hello", "World", ""]
String.join(", ", "A", "B", "C"); // Returns: "A, B, C"
String.join("-", Arrays.asList("X", "Y", "Z")); // Returns: "X-Y-Z"
Indexing & Searchingโ
s.charAt(4); // Returns: 'l'
s.indexOf("o"); // Returns: 4 (first occurrence)
s.lastIndexOf("o"); // Returns: 7 (last occurrence)
s.indexOf("World", 5); // Search starting from index 5
Conversionโ
String.valueOf(123); // Returns: "123"
String.valueOf(3.14); // Returns: "3.14"
String.valueOf(true); // Returns: "true"
String.valueOf(new char[]{'A', 'B'}); // Returns: "AB"
"42".getBytes(); // Returns: byte array
๐ Quick Reference Summaryโ
| Class | Key Methods | Primary Use Cases |
|---|---|---|
| Integer | parseInt(), valueOf(), toString(), max(), min(), bitCount() | String conversion, math ops, bit manipulation |
| Long | parseLong(), valueOf(), toString(), max(), min() | Large number operations, bit manipulation |
| Character | isDigit(), isLetter(), toUpperCase(), toLowerCase() | Character validation and transformation |
| Boolean | parseBoolean(), valueOf(), logicalAnd(), logicalOr() | Boolean parsing and logical operations |
| Double/Float | parseDouble(), isNaN(), isInfinite(), compare() | Decimal number operations, special value checking |
| String | trim(), split(), contains(), substring(), replace() | Text manipulation, searching, formatting |
๐ฏ Best Practicesโ
- Prefer
valueOf()over constructors - Uses cached instances for small numbers - Handle
NumberFormatException- Always catch when parsing strings - Use
equals()for object comparison - Don't use==with wrapper objects - Consider autoboxing/unboxing overhead - Be mindful in performance-critical code
- Null-safe operations - Wrapper objects can be null, primitives cannot
// Good practices
Integer num = Integer.valueOf("123"); // Uses cache
if (Objects.equals(num1, num2)) { } // Null-safe comparison
// Avoid
Integer num = new Integer("123"); // Deprecated, creates new instance
if (num1 == num2) { } // Unsafe reference comparison